<template>
  <el-dialog :title="form.id ? '编辑' : '新增'" v-model="visible" :close-on-click-modal="false" draggable>
    <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
      <el-row :gutter="24">
        <el-col :span="24" class="mb20">
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入名称" clearable show-word-limit :maxlength="64" />
          </el-form-item>
        </el-col>

        <el-col :span="24" class="mb20">
          <el-form-item label="国家码" prop="code">
            <el-input v-model="form.code" placeholder="请输入国家码" clearable show-word-limit />
          </el-form-item>
        </el-col>

        <el-col :span="24" class="mb20">
          <el-form-item label="语言代码" prop="lang">
            <el-input v-model="form.lang" placeholder="请输入语言代码" clearable show-word-limit :maxlength="8" />
          </el-form-item>
        </el-col>

        <el-col :span="24" class="mb20">
          <el-form-item label="国旗图片" prop="image">
            <el-input v-model="form.image" placeholder="请输入国旗图片" clearable show-word-limit :maxlength="125" />
          </el-form-item>
        </el-col>

        <el-col :span="24" class="mb20">
          <el-form-item label="是否热门" prop="isHot">

            <el-select v-model="form.isHot" placeholder="请选择是否热门" clearable>
              <el-option :value="false" label="否"></el-option>
              <el-option :value="true" label="是"></el-option>
            </el-select>
            <!-- <el-input v-model="form.isHot" placeholder="请输入是否热门" clearable show-word-limit /> -->
          </el-form-item>
        </el-col>

        <el-col :span="24" class="mb20">
          <el-form-item label="排序" prop="sort">
            <el-input v-model="form.sort" placeholder="请输入排序" clearable show-word-limit />
          </el-form-item>
        </el-col>

        <el-col :span="24" class="mb20">
          <el-form-item label="洲编码" prop="continentCode">
            <el-input v-model="form.continentCode" placeholder="请输入洲编码" clearable show-word-limit :maxlength="32" />
          </el-form-item>
        </el-col>

        <el-col :span="24" class="mb20">
          <el-form-item label="国家编码" prop="countryCode">
            <el-input v-model="form.countryCode" placeholder="请输入国家编码" clearable show-word-limit :maxlength="32" />
          </el-form-item>
        </el-col>

        <el-col :span="24" class="mb20">
          <el-form-item label="国家名称" prop="countryName">
            <el-input v-model="form.countryName" placeholder="请输入国家名称" clearable show-word-limit :maxlength="128" />
          </el-form-item>
        </el-col>

      </el-row>
    </el-form>
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="visible = false">取消</el-button>
        <el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup lang="ts" name="CountryDialog">
import { useDict } from '/@/hooks/dict';
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj } from '/@/api/bizs/Country'
import { rule } from '/@/utils/validate';
const emit = defineEmits(['refresh']);

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
// 定义字典

// 提交表单数据
const form = reactive({
  id: '',
  name: '',
  code: '',
  lang: '',
  image: '',
  isHot: '',
  sort: '',
  continentCode: '',
  countryCode: '',
  countryName: '',
});

// 定义校验规则
const dataRules = ref({
  name: [{ required: true, message: '名称不能为空', trigger: 'blur' }],
  code: [{ required: true, message: '国家码不能为空', trigger: 'blur' }, { validator: rule.number, trigger: 'blur' }],
  lang: [{ required: true, message: '语言代码不能为空', trigger: 'blur' }],
  image: [{ required: true, message: '国旗图片不能为空', trigger: 'blur' }],
  isHot: [{ required: true, message: '是否热门不能为空', trigger: 'blur' }],
  sort: [{ required: true, message: '排序不能为空', trigger: 'blur' }],
  continentCode: [{ required: true, message: '洲编码不能为空', trigger: 'blur' }],
  countryCode: [{ required: true, message: '国家编码不能为空', trigger: 'blur' }],
  countryName: [{ required: true, message: '国家名称不能为空', trigger: 'blur' }],
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.id = ''

  // 重置表单数据
  nextTick(() => {
    dataFormRef.value?.resetFields();
  });

  // 获取country信息
  if (id) {
    form.id = id
    initFormData(id)
  }
};

// 提交
const onSubmit = async () => {
  const valid = await dataFormRef.value.validate().catch(() => { });
  if (!valid) return false;

  try {
    loading.value = true;
    form.id ? await putObj(form) : await addObj(form);
    useMessage().success(form.id ? '修改成功' : '添加成功');
    visible.value = false;
    emit('refresh');
  } catch (err: any) {
    useMessage().error(err.msg);
  } finally {
    loading.value = false;
  }
};


// 初始化表单数据
const initFormData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};

// 暴露变量
defineExpose({
  openDialog
});
</script>
